from rest_framework import serializers
from goods.models import Goods, GoodsCategory, Banner, HotSearchWords, DiscountPackage, SearchAd, \
    GoodsBannerImage, GoodsDescImage, Brand, GoodsAttribute

from .search_indexes import GoodsIndex


def time_format():
    """时间格式化"""
    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    update_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    return create_time, update_time


class CategorySerializer(serializers.ModelSerializer):
    """首页导航分类"""

    class Meta:
        model = GoodsCategory
        fields = ("id", "name", "image")


class GoodsBannerImageSerializer(serializers.ModelSerializer):
    """商品详情页轮播图"""

    class Meta:
        model = GoodsBannerImage
        fields = ("id", "banner_image")


class GoodsDescImageSerializer(serializers.ModelSerializer):
    """商品详情页详情图"""

    class Meta:
        model = GoodsDescImage
        fields = ("id", "desc_image")


class GoodsBrandSerializer(serializers.ModelSerializer):
    """商品品牌"""

    class Meta:
        model = Brand
        fields = ("id", "name")


class GoodsIndexSerializer(serializers.ModelSerializer):
    """首页分类显示"""

    class Meta:
        model = Goods
        fields = ["id", "name", "goods_front_image", "market_price", "shop_price"]


class GoodsColorSerializer(serializers.ModelSerializer):
    """商品颜色"""
    goods_id = serializers.PrimaryKeyRelatedField(label='商品', read_only=True)

    class Meta:
        model = GoodsAttribute
        fields = ["id", "goods_id", "color", ]


class GoodsAttributeSerializer(serializers.ModelSerializer):
    """商品属性"""
    # PrimaryKeyRelatedField 此字段将被序列化为关联对象的主键。
    goods_id = serializers.PrimaryKeyRelatedField(label='商品', read_only=True)

    class Meta:
        model = GoodsAttribute
        fields = ["id", "goods_id", "goods_code", "color", "image", "size", "price", "storage_amout", ]


class GoodsSerializer(serializers.ModelSerializer):
    """商品"""
    category = CategorySerializer()
    brand = GoodsBrandSerializer()
    banner_images = GoodsBannerImageSerializer(many=True)
    desc_images = GoodsDescImageSerializer(many=True)
    relevance_goods = GoodsIndexSerializer(many=True)
    create_time, update_time = time_format()

    class Meta:
        model = Goods
        exclude = ["goods_status", "is_hot", "is_new", "is_highpoint", "is_index", ]


class CategoryIndexSerializer(serializers.ModelSerializer):
    """首页分类及显示下面的产品"""

    class Meta:
        model = GoodsCategory
        fields = ["id", "name", ]


class GoodsSearchSerializer(serializers.ModelSerializer):
    """搜索列表"""

    class Meta:
        model = Goods
        fields = ["id", "name", "goods_front_image", "market_price", "shop_price", "sold_num"]


class NewGoodsSerializer(serializers.ModelSerializer):
    """新品专享榜"""

    class Meta:
        model = Goods
        fields = ["id", "name", "goods_front_image", "market_price", "shop_price", "sold_num"]


class HotGoodsSerializer(serializers.ModelSerializer):
    """热销榜"""

    class Meta:
        model = Goods
        fields = ["id", "name", "goods_front_image", "market_price", "shop_price", "sold_num"]


class HighpointGoodsSerializer(serializers.ModelSerializer):
    """高端榜"""

    class Meta:
        model = Goods
        fields = ["id", "name", "goods_front_image", "market_price", "shop_price", "sold_num"]


class BannerSerializer(serializers.ModelSerializer):
    """首页轮播图"""
    # PrimaryKeyRelatedField 此字段将被序列化为关联对象的主键。
    goods_id = serializers.PrimaryKeyRelatedField(label='商品', read_only=True)

    class Meta:
        model = Banner
        fields = ["id", "goods_id", "image", ]


class SearchAdSerializer(serializers.ModelSerializer):
    """搜索框下方广告"""

    class Meta:
        model = SearchAd
        fields = ["id", "title", "image"]


class HotWordsSerializer(serializers.ModelSerializer):
    """热搜词"""

    class Meta:
        model = HotSearchWords
        fields = ["id", "keywords", "index"]


class DiscountPackageSerializer(serializers.ModelSerializer):
    """优惠套餐"""
    goods = GoodsIndexSerializer(many=True)
    total_price = serializers.SerializerMethodField(label='合计总价')

    class Meta:
        model = DiscountPackage
        fields = ["id", "name", "image", "goods", "start_time", "end_time", "saved_at_most", "total_price"]

    def get_total_price(self, obj):
        """固定写法:get_+额外添加的字段名,obj代表DiscountPackage实例对象"""
        result = sum([i.shop_price for i in obj.goods.all()]) - obj.saved_at_most
        return result


# elasticsearch
from drf_haystack.serializers import HaystackSerializer


class GoodsElasticsearchIndexSerializer(HaystackSerializer):
    """
    Goods索引结果数据序列化器
    """

    class Meta:
        index_classes = [GoodsIndex]
        fields = ('text', 'id', 'name', 'goods_front_image', 'market_price', 'shop_price', 'sold_num')
